<?php
namespace payClass\pay;

/**
 * 商户配置
 */
class Config
{

    // 当前文件夹名
    public static $folder;
    // 商户号
    public static $merchant = '88882018122210000080';
    // 密钥
    public static $key      = '414292c2d851f6acff2a6a9b75d8c0c2';
    // 同步回调（通知）地址
    public static $merUrl;
    // 异步回调（通知）地址
    public static $notifyUrl;

    function __construct()
    {
        self::$folder    = substr(__DIR__, strrpos(__DIR__, '\\')+1);
        self::$merUrl    = 'http://'.$_SERVER['HTTP_HOST'];
        self::$notifyUrl = self::$merUrl . DIRECTORY_SEPARATOR . 'payClass' . self::$folder . DIRECTORY_SEPARATOR . 'sycNotice.php';
    }

    public static function getSignMsg($pay_params = array(),$paySecret) {//签名
        $params_str = "";
        $signMsg    = "";
        ksort($pay_params);//以参数名的字典顺序排序

        foreach ( $pay_params as $key => $val ) {
            if ($key != "sign" && isset ( $val ) && @$val != "") {
                $params_str .= $key . "=" . $val . "&";
            }
        }
        $params_str .= "paySecret=" . $paySecret;
        $signMsg    = strtoupper(md5($params_str));
        return $signMsg;
    }

    public static function payQueryTest($param=[]){//代付查询
        //初始化
        $curl = curl_init();
        //设置网关的url
        curl_setopt($curl, CURLOPT_URL, 'http://agent.epay777.com/rb-pay-web-merchant/agentPay/query');
        curl_setopt($curl, CURLOPT_POST, 1 );
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        //设置请求参数
        $post_data = array(
            "payKey"   => self::$merchant,//商户的支付key,用于标识商户身份。可登陆商户门户，在账户信息中可以查看（对应商户号）
            "orderNo"  => $param['order'],//订单号编号
            "signType" => "MD5" //签名算法，固定MD5
        );
        $post_data["sign"] = self::getSignMsg($post_data,self::$key);

        //print_r($post_data);

        //设置post表单方式提交
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));

        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        $result = json_decode($data);//转换为json对象

        return get_object_vars($result);
    }

    //发送请求
    public static function payTest($param=[]){//代付
        //初始化
        $curl = curl_init();
        //设置网关的url
        curl_setopt($curl, CURLOPT_URL, 'http://agent.epay777.com/rb-pay-web-merchant/agentPay/pay');
        curl_setopt($curl, CURLOPT_POST, 1 );
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        //设置请求参数
        $post_data = array(
            "payKey"          => self::$merchant,//商户的支付key,用于标识商户身份。可登陆商户门户，在账户信息中可以查看（对应商户号）
            "orderNo"         => $param['order'],//订单号编号，商户必须保证它的唯一性
            "bankAccountName" => $param['account_Name'],//收款人姓名
            "bankAccountNo"   => $param['account_Number'],//收款人银行账号
            "bankCode"        => $param['bank_Code'],//收款银行编码,具体见接口文档
            "settAmount"      => $param['amount'],//金额，单位元,保留2位小数
            "signType"        => "MD5" //签名算法，固定MD5
        );

        $post_data["sign"] = self::getSignMsg($post_data,self::$key);

        //print_r($post_data);

        //设置post表单方式提交
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));

        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        $result = json_decode($data);//转换为json对象

        return get_object_vars($result);
    }
}